#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cocos2d.h"
#include "cocoapp/HelloWorldScene.h"


#include <QtUiTools>
#include "CCQTSceneDirector.h"

#include "output/cswriter.h"
#include "output/csreader.h"
#include "constantManager.h"

using namespace cocos2d;

MainWindow::MainWindow(CCEGLView *eglView , QWidget * parent):
    QMainWindow(parent),ui(new Ui::MainWindow)
{
    m_eglView = eglView;
    ui->setupUi(this);
    QMetaObject::connectSlotsByName(this);
    setWindowTitle("Cocos2d-X Scene Editor v0.1");
    this->setStyle(new QPlastiqueStyle);
    
    m_treeWidget_nodes = qFindChild<TreeWidget_CCScene*>(this,  "treeWidget_nodes");
    m_listWidget_res = qFindChild<QListWidget*>(this ,  "listWidget_res");
    m_cocos2dwindow = qFindChild<CocoWindow*>(this ,  "cocos2dwindow");
    m_peditor_container = qFindChild<PEditors_CCProperties*>(this ,  "peditor_container");
    QAction *actionAbout_Qt = qFindChild<QAction*>(this, "actionAbout_Qt");

    
    //toolbox 
    //cocos2d -x window 
 	//m_cocos2dwindow->setFixedHeight( size.height);
    // controller .................
//    TreeWidget_CCScene  * ccscene_controller = new TreeWidget_CCScene(this); 
    QHBoxLayout *layout = new QHBoxLayout(m_cocos2dwindow);
    layout->setMargin(0);
    layout->addWidget(m_eglView->m_widget);

 	CCSize size = m_eglView->getSize();
	m_cocos2dwindow->setMinimumHeight(size.height);
    statusBar()->showMessage(tr("Ready"));
    
    //about qt .
    connect(actionAbout_Qt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::action_export()
{
	
    QString filename = QFileDialog::getSaveFileName(this , "Save Scene ", QDir::currentPath() , "Coco2d Scene files (*.ccs);;All files(*.*)" );
    if (!filename.isNull())
    { 
        CSWriter::sharedWriter()->writeCSC(filename.toLocal8Bit().data());
		QMessageBox::information(this , "Information : File Confirmation " , "Scene File "+ filename +" saved !" , QMessageBox::Ok);
    }
    
}
int MainWindow::askSave()
{
	//if current workspace not saved.
	if (!ConstantManager::sharedManager()->getSaved())
	{
		//ask user if save the current workspace. 
		QMessageBox msgbox("warning" , "The Workspace was not saved . Save the workspace ?", QMessageBox::Question , QMessageBox::Save , QMessageBox::Ignore , QMessageBox::Cancel ,this);
		int ret = msgbox.exec();
		//if yes
		//open file dialog ,save the current workspace.
		if (ret == QMessageBox::Save)
		{
			return save();
		}
		return  ret;
	}
	return QMessageBox::Save;
}


void MainWindow::action_openproj()
{
    QString filename = QFileDialog::getOpenFileName(
       this,
       "Open Project",
       QDir::currentPath(),
       "Xml files (*.xml );;All files(*.*)");
    if (!filename.isNull())
    { 
		if (askSave() == QMessageBox::Cancel)
			return;
		clearWorkSpace();    
		CSReader::shareReader()->readCSCProjFile(filename.toLocal8Bit().data());
		ConstantManager::sharedManager()->setCurrentProjPath(filename);
		ConstantManager::sharedManager()->setSaved(true);
    } 
}

void MainWindow::action_new()
{
	if (askSave() == QMessageBox::Cancel)
		return;
	clearWorkSpace();    

}

void MainWindow::clearWorkSpace()
{
    //clear the workspace.| | remove the treewidget Items and scene trees .

	m_treeWidget_nodes->clearWorkSpace();    
    ConstantManager::sharedManager()->setCurrentProjPath(0);
}

void MainWindow::action_openParticleEditor()
{
    
}


 
int MainWindow::save()
{
	if (ConstantManager::sharedManager()->getCurrentProjPath()== 0)
	{
		return saveas();
	}else
	{
		CSWriter::sharedWriter()->writeCSCProjFile(ConstantManager::sharedManager()->getCurrentProjPath().toLocal8Bit().data());
		ConstantManager::sharedManager()->setSaved(true);
		return QMessageBox::Save;
	}
}

int MainWindow::saveas()
{
    // open save file dialog.
    QString filename = QFileDialog::getSaveFileName(this , "Save Project", QDir::currentPath() , "Xml files (*.xml);;All files(*.*)" );
    if (!filename.isNull())
    { 
        CSWriter::sharedWriter()->writeCSCProjFile(filename.toLocal8Bit().data());
        ConstantManager::sharedManager()->setCurrentProjPath(filename);
		ConstantManager::sharedManager()->setSaved(true);
        QMessageBox::information(this , "Information : File Confirmation " ,  filename +"  saved !" , QMessageBox::Ok);
		return true;
    }else
	{
		ConstantManager::sharedManager()->setSaved(false);
		return QMessageBox::Cancel;
	}

}


void MainWindow::action_save()
{
	save();
	
}

void MainWindow::action_saveas()
{
    saveas();
}

void MainWindow::closeWindow()
{ 
    //release all the data .
    clearWorkSpace();
    exit(0);
}

void MainWindow::action_close()
{
    //ask user if save the current workspace. 
    //if yes
    //clear the workspace.||| remove the treewidget Items and scene trees .
    //ask user if save the current workspace.
	if (askSave() == QMessageBox::Cancel)
		return;
     closeWindow();
}



void MainWindow::changeEvent(QEvent *event)
{
    //QMessageBox::information(this , "" , "" + event->type() , QMessageBox::Ok) ;
    //qDebug()<<  event->type() ;
    switch(event->type())
    {
        case QEvent::ActivationChange :
        if (this->isActiveWindow())
        {
            CCDirector::sharedDirector()->resume();
        }else
        {
            //CCDirector::sharedDirector()->pause();
        }
        break;
    case QEvent::WindowStateChange:
    {
        if (this->isMinimized())
        {
            qDebug()<<  "minimized";
            CCDirector::sharedDirector()->pause();
        }
    }
    }
    
}
 

